fail_build ()
{
	echo BUILD FAILED
	exit 1
}

REVISION=`svn info | awk '/^Revision:/ { print $2 }'`
RELEASE=`cat src/build/next_release_number`
CRUISE_PASSWORD=`cat /etc/cruise/googlecodepassword`

rm -rf target
mkdir target

echo "Releasing version $RELEASE from trunk revision $REVISION"

mv fetched-artifacts/hypirinha.jar target/hypirinha-$RELEASE.jar
if [ $? -ne 0 ]; then
	fail_build
fi

mv fetched-artifacts/hypirinha-src.jar target/hypirinha-src-$RELEASE.jar
if [ $? -ne 0 ]; then
	fail_build
fi

svn cp https://hypirinha.googlecode.com/svn/trunk/java@$REVISION https://hypirinha.googlecode.com/svn/tags/java/$RELEASE --non-interactive --no-auth-cache --username cruise@hypirinha.org --password $CRUISE_PASSWORD -m "Tag for release $RELEASE"
if [ $? -ne 0 ]; then
	fail_build
fi

python src/build/python/googlecode_upload.py -p hypirinha -u cruise@hypirinha.org -w $CRUISE_PASSWORD -s "Self-contained JAR, release $RELEASE" -l "Type-Java-Library,Featured" target/hypirinha-$RELEASE.jar
if [ $? -ne 0 ]; then
	fail_build
fi

python src/build/python/googlecode_upload.py -p hypirinha -u cruise@hypirinha.org -w $CRUISE_PASSWORD -s "Source code, including generated source code, release $RELEASE" -l "Type-Source,Featured" target/hypirinha-src-$RELEASE.jar
if [ $? -ne 0 ]; then
	fail_build
fi

echo BUILD SUCCESSFUL
exit 0